package com.ruoyi.community.service.impl;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.constant.redisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.community.mapper.CommunityPostMapper;
import com.ruoyi.community.domain.CommunityPost;
import com.ruoyi.community.service.ICommunityPostService;

/**
 * 社区帖子Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-22
 */
@Service
public class CommunityPostServiceImpl implements ICommunityPostService {
    @Autowired
    private CommunityPostMapper communityPostMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询社区帖子
     *
     * @param id 社区帖子主键
     * @return 社区帖子
     */
    @Override
    public CommunityPost selectCommunityPostById(Long id) {
        return communityPostMapper.selectCommunityPostById(id);
    }

    /**
     * 查询社区帖子列表
     *
     * @param communityPost 社区帖子
     * @return 社区帖子
     */
    @Override
    public List<CommunityPost> selectCommunityPostList(CommunityPost communityPost) {
        return communityPostMapper.selectCommunityPostList(communityPost);
    }

    /**
     * 新增社区帖子
     *
     * @param communityPost 社区帖子
     * @return 结果
     */
    @Override
    public int insertCommunityPost(CommunityPost communityPost) {
        return communityPostMapper.insertCommunityPost(communityPost);
    }

    /**
     * 修改社区帖子
     *
     * @param communityPost 社区帖子
     * @return 结果
     */
    @Override
    public int updateCommunityPost(CommunityPost communityPost) {
        return communityPostMapper.updateCommunityPost(communityPost);
    }

    /**
     * 批量删除社区帖子
     *
     * @param ids 需要删除的社区帖子主键
     * @return 结果
     */
    @Override
    public int deleteCommunityPostByIds(Long[] ids) {
        return communityPostMapper.deleteCommunityPostByIds(ids);
    }

    /**
     * 删除社区帖子信息
     *
     * @param id 社区帖子主键
     * @return 结果
     */
    @Override
    public int deleteCommunityPostById(Long id) {
        return communityPostMapper.deleteCommunityPostById(id);
    }

    @Override
    public int liked(Long id) {
        Long likedOld = selectCommunityPostById(id).getGoods();
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Double score = redisTemplate.opsForZSet().score(redisKey.COMMUNITY_LIKED + id, String.valueOf(userId));
        if (score == null) {
            //存进redis
            redisTemplate.opsForZSet().add(redisKey.COMMUNITY_LIKED + id, String.valueOf(userId), System.currentTimeMillis());
            CommunityPost communityPost = new CommunityPost();
            communityPost.setId(id);
            communityPost.setGoods(likedOld + 1);
            int i = updateCommunityPost(communityPost);
            return i > 0 ? 2 : 0;
        } else {
            //移除members
            redisTemplate.opsForZSet().remove(redisKey.COMMUNITY_LIKED + id, String.valueOf(userId));
            CommunityPost communityPost = new CommunityPost();
            communityPost.setId(id);
            communityPost.setGoods(likedOld - 1);
            int i = updateCommunityPost(communityPost);
            return i > 0 ? 1 : 0;
        }
    }
}
